package edu.neusoft.yuejian_09.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.activity.ShowInfoActivity;
import edu.neusoft.yuejian_09.bean.Show;
import it.sephiroth.android.library.picasso.Picasso;

import static edu.neusoft.yuejian_09.base.Constant.PHOTO_URL;

/**
 * Created by 爽 on 2017/11/1.
 */

public class ClubShowsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater1;

    public ClubShowsAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater1 = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Show entity = (Show) mDataList.get(position);
        if (null == entity)
            return;
        //绑定数据
        ClubShowsAdapter.ViewHolder viewHolder = (ClubShowsAdapter.ViewHolder) holder;
        viewHolder.show_content.setText(entity.getS_content());
        viewHolder.show_address.setText(entity.getS_address());
        viewHolder.time.setText(entity.getS_date());
        viewHolder.user_name.setText(entity.getUser_name());
        // System.out.printf("activity_name");
        //viewHolder.price.setText("￥"+entity.getTrain_price()+ "元");
        String user_pic=PHOTO_URL+entity.getUser_pic();
        String show_pic =PHOTO_URL+entity.getS_pic();
        Picasso.with(mContext).load(user_pic).into(viewHolder.user_pic);
        if(viewHolder.user_pic.getDrawable()==null){
            viewHolder.user_pic.setImageResource(R.drawable.no_userpic);
        }
        Picasso.with(mContext).load(show_pic).into(viewHolder.show_pic);
        if(viewHolder.show_pic.getDrawable()==null){
            viewHolder.show_pic.setImageResource(R.drawable.no_pic);
        }
        //点击item跳转
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putInt("s_id",entity.getS_id());
                bundle.putInt("user_id",entity.getUser_id());
                bundle.putString("s_content",entity.getS_content());
                //bundle.putInt("s_address",entity.getTrain_price());
                bundle.putString("s_address",entity.getS_address());
                Intent intent=new Intent(mContext, ShowInfoActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });


    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建ViewHolder
        View v = mLayoutInflater1.from(parent.getContext()).inflate(R.layout.cardview_show, parent, false);
        return new ClubShowsAdapter.ViewHolder(v);

    }

    //自定义的V
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView user_pic,show_pic;
        TextView user_name,show_content,show_address,time;

        public ViewHolder(View itemView) {
            super(itemView);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            user_pic = (ImageView) itemView.findViewById(R.id.user_pic);
            show_content= (TextView) itemView.findViewById(R.id.content);
            show_pic= (ImageView) itemView.findViewById(R.id.showlist_pic);
            show_address=(TextView)itemView.findViewById(R.id.address);
            time=(TextView)itemView.findViewById(R.id.time);
        }
    }
}
